package nz.ac.massey.jbuif.editor.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;

import nz.ac.massey.jbuif.editor.BeanEditor;


/**
 * Class ObjectEditorPanel
 * @author AMIR H KIOUMARS
 *
 */
public class ObjectEditorPanel extends BaseComponent implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JComboBox objectComboBox;
	@SuppressWarnings("unused")
	private JButton objectButton;
	private String propertyType;
	private JLabel currentValue;

	
	/**
	 * ObjectEditorPanel
	 * @param p bean editor instance
	 * @param name string name
	 * @param displayName string name to be display
	 * @param pev an object
	 */
	public ObjectEditorPanel(BeanEditor p, String name,String displayName,Object pev) {
		super(p, name, displayName, pev);
				
		currentValue = new JLabel();
		setTextField();
		
		objectComboBox = new JComboBox();
		objectComboBox.addItem("null");
		objectComboBox.addItem("Use existing "+name);
		objectComboBox.addItem("New "+name);
		objectComboBox.addItem("Edit "+name);
		objectComboBox.setActionCommand("ComboBox");
			
		//If Object is something, allow edit
		if(super.getPropertyValue()!=null){
			objectComboBox.setSelectedIndex(3);
		}
		objectComboBox.addActionListener(this);
		super.addPropertyComponent(currentValue);
		super.addPropertyComponent(objectComboBox);		
	}	
	
	
	/**
	 * setTextField set the text field with a proper message
	 */
	public void setTextField(){
		if(super.getPropertyValue()!=null){
			currentValue.setText(super.getPropertyValue().toString());
		} else {
			currentValue.setText("No value set");
		}
	}
	
	
	/**
	 * getSelectedIndex get the combo box selected item index
	 * @return the selected index of combo box
	 */
	public int getSelectedIndex(){
		return objectComboBox.getSelectedIndex();
	}
		
	
	/**
	 * actionPerformed controls the action performed
	 * @param e ActionEvent instance
	 * 
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().matches("ComboBox")){
			
			ActionEvent e1 = new ActionEvent(this, e.hashCode(), "comboClicked");
			e1.setSource(this);
			
			super.getBeanEditor().actionPerformed(e1);
		}
	}
	
	
	/**
	 * getPropertyValue get the property value
	 * @param object the property value
	 */
	public Object getPropertyValue(){
		return super.getPropertyValue();
	}
	
	
	/**
	 * setPropertyType set the property type
	 * @param propertyType string property type
	 */
	public void setPropertyType(String propertyType){
		this.propertyType = propertyType;
	}
	
	
	/**
	 * getPropertyType get the property type
	 * @return property type
	 */
	public String getPropertyType(){
		return this.propertyType;
	}
	
	
	/**
	 * redrawText redraw the text field
	 */
	public void redrawText(){
		setTextField();
		this.repaint();
		this.validate();
	}
}
